package belote.webapp.jsf.bean;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name="env", eager=true)
@ApplicationScoped
public class EnvironnementBean {

	public EnvironnementBean() {
		System.out.println("Construction de " + getClass().getSimpleName());
	}
	
	public String getApplicationRootPath() {
		
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		StringBuilder sbApplicationRootPath = new StringBuilder();
		
		sbApplicationRootPath.append(request.getScheme()).append("://").append(request.getServerName());
		if (("http".equals(request.getScheme()) && request.getServerPort() != 80) || ("https".equals(request.getScheme()) && request.getServerPort() != 443)) {
			sbApplicationRootPath.append(":").append(request.getServerPort());
		}
		
		sbApplicationRootPath.append(request.getContextPath());
		
		return sbApplicationRootPath.toString();
	}
	
	public String getStaticRootPath() {
		
		return getApplicationRootPath() + "/static";
	}
}
